<template>
<div class="tree">
  <el-tree
   :data="treeData"
   :props="defaultProps" 
   :current-node-key="currentNodekey" 
   :show-checkbox="showcheckbox" 
   lazy 
   :load="loadNode" 
   @node-click="handleNodeClick">
  </el-tree>
</div>
</template>

<script>
import axios from 'axios'
import { constants } from 'crypto';
export default {
  props: {
    showcheckbox: {
      type: Boolean,
      required: false,
      default: false
    }
  },
  name: "Tree",
  data() {
    return {
      treeData: [],
      defaultProps: {
        id: 'id',
        label: "label"
      },
      currentNodekey: ""
    };
  },
  mounted() {
    // this.loadNode()
    // 跳转到第一个菜单
    if (this.treeData.length > 0) {
      this.$nextTick(() => {
        this.$store.dispatch("selected", this.treeData[0]);
        // this.currentNodekey = this.treeData[0].id;
        // console.log(this.currentNodekey)
        // this.$refs.tree.setCurrentKey(this.currentNodekey);
      })
    }
  },
  methods: {
    loadNode(node, resolve) {
      //console.log(node)
      //console.log(node.data)
        node.data.children = []
        // 调接口查询资料树结构
        let info = {
          userId: '0001',
          auditPrjId: '00000001',
          auditGrpId: '10000001',
          parentNodeCd: node.data.id?node.data.id:'0'
        }
        axios.post('http://localhost:7111/prjInf/queryTOesDocTree', info).then((response) => {
        // axios.post('http://128.16.177.136:7111/prjInf/queryTOesDocTree', info).then((response) => {
          console.log(response)
          if (response.data && response.data.tOesDocTreeList && response.data.tOesDocTreeList.length > 0) {
            let arr = []
            response.data.tOesDocTreeList.forEach(item => {
              arr.push({
                id: item.nodeId,
                label: item.nodeNm,
                parentNodeCd: item.parentNodeCd,
                nodeLvl: item.nodeLvl,
                nodeCd: item.nodeCd
              })
            })
            //console.log(arr)
            node.data.children = arr
            return resolve(node.data.children)
          } else {
            return resolve(node.data.children)
          }
        }).catch((response) => {
          console.log(response)
        })
    },
    generateOptions(params) { //生成Cascader级联数据
      var result = [];
      for (let param of params) {
        if (param.parentNodeCd === '') { //判断是否为顶层节点
          var parent = { //转换成el-Cascader可以识别的数据结构
            'label': param.nodeNm,
            'id': param.nodeId,
            'parentNodeCd': param.parentNodeCd,
            'nodeLvl': param.nodeLvl,
            'nodeCd': param.nodeCd
          }
          parent.children = this.getchilds(param.nodeId, params); //获取子节点
          result.push(parent)
        }
      }
      return result
    },
    getchilds(id, array) {
      let childs = new Array()
      for (let arr of array) { //循环获取子节点
        if (arr.parentNodeCd === id) {
          childs.push({
            'label': arr.nodeNm,
            'id': arr.nodeId,
            'parentNodeCd': arr.parentNodeCd,
            'nodeLvl': arr.nodeLvl,
            'nodeCd': arr.nodeCd
          })
        }
      }
      for (let child of childs) { //获取子节点的子节点
        let childscopy = this.getchilds(child.id, array); //递归获取子节点
        //console.log(childscopy)
        if (childscopy.length > 0) {
          child.children = childscopy
        }
      }
      return childs
    },
    handleNodeClick(data) {
      //console.log(data)
      // if (data.nodeLvl && !(data.nodeLvl === 1)) {
        if (data.nodeCd === '101001') {
          this.$router.push({
            name: 'auditEvidence'
          })
        } else if (data.nodeCd === '000111') {
          this.$router.push({
            name: 'auditevidencedocket'
          })
        } else if (data.nodeCd === '001001') {
          this.$router.push({
            name: 'auditdraft'
          })
        } else if (data.nodeCd === '001110') {
          this.$router.push({
            name: 'auditreport'
          })
        } else if (data.nodeCd === '001111') {
          this.$router.push({
            name: 'outline'
          })
        } else {
          //console.log(data.id)
          this.$router.push({
            name: 'auditcommon',
            query:{label:data.label,nodeId:data.id}
          })
        }
        this.$emit('showTp', JSON.parse(JSON.stringify(data)))
      // }
    }
  }
}
</script>

<style lang="less" scoped>
.tree {
  height: 100%;
}

.el-tree {
  width: 200px;
  height: 100%;
  background: #455f85;
  font-size: 14px;
  line-height: 20px;
  overflow-y: auto;

  &/deep/.el-tree-node__content {
    height: 44px;
    line-height: 20px;
    font-family: PingFangSC-Regular;
    background-color: #455f85;
    color: #fff;
  }

  &/deep/.el-tree-node__content:hover {
    background: #2e4668;
  }

  &/deep/.el-tree-node:focus>.el-tree-node__content {
    background: #58739a;
    color: #eecc13;
  }

  &/deep/.el-tree-node__label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>
